Val Function

Returns the numeric form of a String.

Syntax

result = Val( string )

result = stringVariable.Val


Parameters

string

String

Any valid String expression.



Notes

The Val function stops reading the String at the first character it doesn't recognize as part of a number. All other characters are automatically stripped.

It does recognize prefixes &o (octal), &b (binary), and &h (hexadecimal). However, spaces are not allowed in front of the ampersand. That is, " &hFF" returns 0, but "&h FF" returns 255.

The CDbl function is the same as the Val function but is used when you need to pass a String that uses a character other than the period (.) as the decimal separator. It uses the decimal character specified by the operating system. For example, on Windows XP, it is set in the Regional and Language Options Control Panel. Val should generally be used to convert internal data, but not data entered by the user. Val is not international-savvy, but CDbl is.

The CStr function is the same as the Str function but is used when you need to pass a number that uses a character other than the period (.) as the decimal separator. It uses the decimal character specified by the operating system.

Val returns zero if string contains no numbers.


Examples

These examples use the Val function to return the numbers contained in a String.

Dim n As Integer
n = Val ("12345") //returns 12345
n = Val(" 12345") //returns 12345
n = Val("123 45") //returns 123
n = Val(" &hFFF")  //returns 4095
n = Val(" &b1111")  //returns 15

See Also

, CDbl, CStr, Str functions; &b, &h, &o literals.